package com.tanhua.sso.controller;

import com.tanhua.common.pojo.User;
import com.tanhua.sso.vo.ErrorResult;
import com.tanhua.sso.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/loginVerification")
    public ResponseEntity<Object> Authentlogin(@RequestBody Map<String, String> map) {
        String phone = map.get("phone");
        String code = map.get("verificationCode");
        Map<String, Object> data = userService.login(phone, code);
        if (data != null) {
            return ResponseEntity.ok(data);
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("000002").errMessage("验证码错误").build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    /**
     * 校验token，根据token查询用户数据
     *
     * @param token
     * @return
     */
    @GetMapping("{token}")
    public User queryUserByToken(@PathVariable("token") String token) {
        return this.userService.queryUserByToken(token);
    }
}
